package com.google.code.bankagregator.adapter.bank.alpha.parser;

import com.google.code.bankagregator.adapter.exception.AdapterConnectException;
import com.google.code.bankagregator.entities.*;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kirill
 * Date: 15.06.13
 * Time: 18:24
 * To change this template use File | Settings | File Templates.
 */
public class FuncForCredParsers {

    private String CREDIT_URL = "http://www.alfabank.com.ua/retail/credits/avto";
    private Credit credit;
    public Bank bank = null;

    public FuncForCredParsers() throws AdapterConnectException {
        bank = new Bank();
        bank.setName("ПАО «Альфа-Банк» Украина");
        bank.setUrl("http://www.alfabank.com.ua/");
        credit = new Credit();
    }

    /**
     * Get some credit`s params for the auto credit with different prepayment
     *
     * @param indexRate   - the row which keeps rate
     * @param indexTRrate - the line which keeps prepayment
     * @param indexTDrate - the row which keeps prepayment
     * @return some credit`s params
     * @throws com.google.code.bankagregator.adapter.exception.AdapterConnectException
     *
     */

    public List<Credit> parser(int indexRate, int indexTRrate, int indexTDrate) throws AdapterConnectException {
        Document con = new FuncForDepParsers(CREDIT_URL).con;
        List<Credit> creditParam = new ArrayList<>();
        Integer[] tableLinesWithParam = new Integer[]{5, 7, 9, 11, 13};
        int i = 0;
        for (int line : tableLinesWithParam) {
            Credit credit = new Credit();
            Element monthsEl = con.select("table.data tr").get(line).select("td").first();

            String termStr = monthsEl.text();
            if (termStr.toCharArray().length > 2) {
                String[] ar = termStr.split("-");
                termStr = ar[1];
            }
            Element rateEl = con.select("table.data tr").get(line).select("td").
                    get(indexRate);
            String rateStr = rateEl.text().replaceAll(",", ".").replaceAll("[^0-9.]+", "");

            Element prepaymentEl = con.select("table.data tr").get(indexTRrate).
                    select("td").get(indexTDrate);
            String prepaymentStr = prepaymentEl.text().replaceAll("[^0-9.]+", "");
            i++;

            int term = Integer.parseInt(termStr) * 31;
            double rate = Double.parseDouble(rateStr);
            double prepayment = Double.parseDouble(prepaymentStr);

            credit.setPrepayment(prepayment);
            credit.setTerm(term);
            credit.setCreditRate(rate);
            credit.setMinAmount(0);
            credit.setType(CreditType.AUTO);
            credit.setName("Кредит на приобретение авто");
            credit.setCurrency(Currency.UAH);
            credit.setBank(bank);
            credit.setUrl(CREDIT_URL);
            credit.setState(State.ACTIVE);
            creditParam.add(credit);
        }
        return creditParam;
    }
}
